<template>
  <section class="app-inner">
    <el-card class="header-card">
      <el-form :inline="true" :model="queryParams" size="small" label-width="84px">
        <el-form-item label="设备类型">
          <el-select placeholder="请选择设备类型" v-model="queryParams.deptLevel" filterable :clearable="true">
            <el-option clearable v-for="item in deptLevel" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="设备状态">
          <el-select placeholder="请选择设备状态" v-model="queryParams.deptLevel" filterable :clearable="true">
            <el-option clearable v-for="item in deptLevel" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="设备名称">
          <el-input clearable placeholder="请输入设备名称" v-model="queryParams.name"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="searchData">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="table-card">
      <el-table border :data="tableData" style="width: 100%">
        <el-table-column label="序号" width="100">
          <template slot-scope="scope">
            {{pageSize*(pageNum-1)+scope.$index+1}}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="eqptAddress" label="设备位置">
          <template slot-scope="scope">
            {{ scope.row.eqptAddress.replace(/null/g, '未知') }}
          </template>
        </el-table-column> -->
          <el-table-column prop="name" label="设备编号"></el-table-column>
          <el-table-column prop="name" label="设备名称"></el-table-column>
          <el-table-column prop="name" label="地点"></el-table-column>
          <el-table-column prop="name" label="设备状态"></el-table-column>
          <el-table-column prop="name" label="甲醛"></el-table-column>
          <el-table-column prop="name" label="二氧化碳"></el-table-column>
          <el-table-column prop="name" label="pm2.5"></el-table-column>
          <el-table-column prop="name" label="空气净化器"></el-table-column>
          <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="handleEdit(scope.row)" type="text" size="small">历史数据</el-button>
            <el-button @click="handleDelete(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="total, prev, pager, next"
        @current-change="pageHandleChange"
        :current-page="pageNum"
        :page-size="pageSize"
        :total="totalPageNum"
      ></el-pagination>
    </el-card>
    <classroomInfoModal ref="classroomInfoModal" @ok="modalOk"></classroomInfoModal>
  </section>
</template>

<script>
import classroomInfoModal from './modules/classroomInfoModal'
import { getClassRoomInfoList, deleteClassRoomInfo } from './js/classRoomInfo'
export default {
  name: 'classroomInfo',
  components: { classroomInfoModal },
  data () {
    return {
      pageNum: 1,
      pageSize: 10,
      totalPageNum: 1,
      tableData: [],
      queryParams: {},
      eqptTypeList: [],
      classroomTypeList: []
    }
  },
  mounted () {
    this.getClassRoomInfoList()
  },
  methods: {
    cons (scope) {
      console.log(scope)
    },
    // 查询按钮
    searchData () {
      this.pageNum = 1
      this.getClassRoomInfoList()
    },
    // 新增
    handleAdd () {
      this.$refs.classroomInfoModal.isAdd = true
      this.$refs.classroomInfoModal.add()
    },
    modalOk () {
      this.getClassRoomInfoList()
    },
    // 分页
    pageHandleChange (page) {
      this.pageNum = page
      this.getClassRoomInfoList()
    },
    async getClassRoomInfoList () {
      const params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize
      }
      // this.queryParams,
      const data = Object.assign({}, this.queryParams, params)
      let res = await getClassRoomInfoList(data)
      console.log(res)
      this.tableData = res.data.records
      this.totalPageNum = res.data.total
    },
    // 编辑
    handleEdit (row) {
      this.$refs.classroomInfoModal.isAdd = false
      this.$refs.classroomInfoModal.edit(row)
    },
    // 删除
    handleDelete (row) {
      this.$confirm('您确定删除当前教室, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteFun(row)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async deleteFun (row) {
      let res = await deleteClassRoomInfo(row.id)
      if (res.code === 0) {
        if (this.tableData.length === 1 && this.pageNum > 1) {
          this.pageNum--
        }
        this.$message.success('删除成功!')
        this.getClassRoomInfoList()
      } else {
        this.$message.error(res.msg)
      }
    }
  }
}
</script>

<style scoped></style>
